/**
 * Description: 
 * File: udp_server.c |Time: 2021-04-12 13:48
 */

#include <stdio.h>
#include <stdlib.h>
#include  <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>

#define BUF_SIZE 100
#define ERR(msg) do {   \
    perror(msg);        \
    printf("[err]%s:%s:%d\n", __FILE__, __func__, __LINE__); \
    exit(EXIT_FAILURE); \
} while(0)

int main(int argc, char *argv[])
{
    if (argc < 3) {
        printf("Usage: %s <ip> <port>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    char buf[BUF_SIZE] = {0};

    struct sockaddr_in multicastaddr;
    socklen_t addrlen = sizeof(struct sockaddr);
    multicastaddr.sin_family = AF_INET;
    multicastaddr.sin_port = htons(atoi(argv[2]));
    multicastaddr.sin_addr.s_addr = inet_addr(argv[1]);

    while(1) {
        printf("[send multicast msg]:");
        fgets(buf, BUF_SIZE, stdin);
        buf[strlen(buf) - 1] = '\0';
        sendto(sockfd, buf, sizeof(buf), 0, 
            (struct sockaddr *)&multicastaddr, addrlen);

    }


    return 0;
}
